#include "tim.h"

void TIM_Initialization(void)
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure={0};
  TIM_OCInitTypeDef TIM_OCInitStructure={0};

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);

  TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
  TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
  TIM_TimeBaseInitStructure.TIM_Period=0x3E8;
  TIM_TimeBaseInitStructure.TIM_Prescaler=0x03;
  TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0x00;
  TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);

  TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;
  TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
  TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;
  TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_Low;
  TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
  TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Disable;
  TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse=0x32;

	TIM_OC1Init(TIM3,&TIM_OCInitStructure);
  TIM_OC2Init(TIM3,&TIM_OCInitStructure);

	TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);
  TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);
	TIM_ARRPreloadConfig(TIM3,ENABLE);
	//清中断，以免一启用中断后立即产生中断
	TIM_ClearFlag(TIM3,TIM_FLAG_Update);
	//使能TIM3中断源
	TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
	TIM_Cmd(TIM3,DISABLE);
	TIM_CtrlPWMOutputs(TIM3,ENABLE); //使能PWM输出
}